<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="query">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button :disabled="submissionFlag" type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表  -->
    <el-table :data="writers" highlight-current-row v-loading="listLoading"  @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="writerId" label="作家编号" width="80" >
      </el-table-column>
      <el-table-column prop="writerName" label="作家名称" width="120" >
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间" width="180" >
      </el-table-column>
      <el-table-column prop="creator" label="创建人" width="600" >
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button :disabled="submissionFlag" size="small" @click="handleEdit(scope.$index, scope.row)" >编辑</el-button>
          <el-button :disabled="submissionFlag" type="danger" size="small" @click="handleDel(scope.$index, scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="作家名称" prop="writerName">
          <el-input v-model="saveForm.writerName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="saveForm.creator" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'

export default {
  data() {
    return {
      // 是否置灰
      submissionFlag:false,
      // 高级查询
      filters: {
        keyword: ''
      },
      // 列表数据
      writers: [],
      // 总条数
      totals: 0,
      // 当前页条数
      pageSize: 5,
      // 当前页
      currentPage:1,
      // 页面忙等
      listLoading: false,
      sels: [],//列表选中列

      //新增界面是否显示
      saveFormVisible: false,
      // 新增忙等
      saveLoading: false,
      // 新增表单校验
      saveFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //新增界面数据
      saveForm: {
        // id
        writerId:0,
        // name
        writerName:"",
        // 创建人
        creator: ""
      }
    }
  },
  methods: {
    query() {
      // 高级查询定位第一页
      this.currentPage = 1;
      this.getWriters();
    },
    //性别显示转换
    formatSex: function (row, column) {

    },
    // 点击页数
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getWriters();
    },

    //获取部门列表
    getWriters() {
      // 获取权限
      var writer = sessionStorage.getItem('writer');
      // console.log("前"+writer);
      // 判断是否有权限
      if (writer == 1){
        // 有我则不置灰
        this.submissionFlag = false;
      } else {
        // 没有则置灰
        this.submissionFlag = true;
      }
      // 携带到后端的参数
      let para = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        keyword: this.filters.keyword
      };
      // 忙等
      this.listLoading = true;
      //NProgress.start();
      // 发送axios请求获取后端数据
      this.$http.post("/writer",para)
        .then(result=>{
          // 关闭忙等
          this.listLoading = false;
          console.log(result);
          // 列表数据赋值
          this.writers = result.data.rows;
          // totals赋值
          this.totals = result.data.totals;
          // console.log(result.data.totals);
        })
        .catch(result=>{
          // 请求失败
          this.$message({
            message: '网络繁忙',
            type: 'error'
          });
        })
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        console.log(row)
        this.listLoading = true;
        //将id携带过去
        console.log(row.writerId);
        this.$http.delete("/writer/"+row.writerId)
          .then(result=>{
            //关闭忙等框
            this.listLoading = false;
            result = result.data;
            //判断

              //跳到第一页，防止页面没数据
              this.currentPage = 1;
              //刷新页面
              this.getWriters();
          })
          .catch(result=>{
            this.$message({
              type: 'error',
              message: '系统错误,我们正在抢救程序员!!'
            });
            //跳到第一页
            this.currentPage = 1;
            //刷新页面
            this.getWriters();
          })
      }).catch(() => {
        //取消就没有了，then才是判断删除与否
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      console.log(row)
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.saveForm = {
        writerId:null,
        // name
        nname:"",
        // 网址
        website: "",
        // 时间
        createtime:"",
        // 创建人
        creator: ""
      };
    },
    //新增and修改
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.saveForm);

            //调整格式
            if(para.parent){
              para.parent={"id":para.parent[para.parent.length-1]}
            }
            // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            this.$http.post("/writer/save",para)
              .then(result=>{
                result = result.data;
                //关闭忙等框
                this.saveLoading = false;
                //跳到第一页，防止页面没数据
                this.currentPage = 1;
                //刷新页面
                this.getWriters();
                this.$refs['saveForm'].resetFields();//重置表单校验
                this.saveFormVisible = false;//关闭模态框
              })
              .catch(result=>{
                //关闭忙等
                this.saveLoading = false;
                this.$message({
                  type: 'error',
                  message: "业务繁忙，我们正在抢救程序员！！！"
                });
                //跳到第一页
                this.currentPage = 1;
                //刷新页面
                this.getWriters();
                this.saveFormVisible = false;//关闭模态框
              });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发请求
        this.$http.patch("/dept",ids)
          .then(result=>{
            //关闭忙等框
            this.listLoading = false;
            //跳到第一页
            this.currentPage = 1;
            //刷新页面
            this.getWriters();
          })
          .catch(result=>{
            this.$message({
              type: 'error',
              message: '系统错误,我们正在抢救程序员!!'
            });
            //跳到第一页
            this.currentPage = 1;
            //刷新页面
            this.getWriters();
          })
      }).catch(() => {
        // 取消什么也不敢
      });
    }
  },
  mounted() {
    this.getWriters();
  }
}

</script>

<style scoped>

</style>
